package com.cictec.middleware.core.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cictec.middleware.commons.mybatis.dao.MybatisDaoUtil;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.mapper.BusMapper;

/**
 * 车辆信息服务
 * 
 * @author ql
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Service
public class BusService {

	private static Logger logger = LoggerFactory.getLogger(BusService.class);

	@Autowired
	private BusMapper busMapper;

	private MybatisDaoUtil<Bus> dao = new MybatisDaoUtil<Bus>();


	/**
	 * 【数据库】获取所有车辆信息
	 * 
	 * @return
	 */
	public List<Bus> selectListAllBus() {
		return selectListBusByParams(null);
	}

	/**
	 * 【数据库】查询当前所有在线车辆
	 * 
	 * @return
	 */
	public List<Bus> selectListAllOnlineBus() {
		Map params = new HashMap();
		params.put("online", true);
		return selectListBusByParams(params);
	}

	/**
	 * 【数据库】根据线路查询车辆(查询所属线路下所有车辆)
	 * 
	 * @param lineUuid
	 * @return
	 */
	public List<Bus> selectBusByLineUuid(String lineUuid) {
		Map params = new HashMap();
		params.put("lineUuid", lineUuid);
		return selectListBusByParams(params);
	}

	/**
	 * 【数据库】查询车辆信息
	 * 
	 * @param busUuid
	 * @return
	 */
	public Bus selectBusByUuid(String busUuid) {
		Map params = new HashMap();
		params.put("busUuid", busUuid);
		return selectOneBusByParams(params);
	}

	/**
	 * 【数据库】根据设备编号查询车辆，包含devCode
	 * 
	 * @param devCode
	 * @return
	 */
	public Bus selectBusByDevCode(String devCode) {
		Map params = new HashMap();
		params.put("devCode", devCode);
		return selectOneBusByParams(params);
	}
	
	/**
	 * 【数据库】根据车牌号查询车辆，包含devCode
	 * 
	 * @param busNumber
	 * @return
	 */
	public Bus selectBusByBusNumber(String busNumber) {
		Map params = new HashMap();
		params.put("busPlateNumber", busNumber);
		return selectOneBusByParams(params);
	}
	public Bus selectBusByBusNumber(Map params) {
		return selectOneBusByParams(params);
	}
	
	public Bus selectOneBusByParams(Map params) {
		return dao.selectOne("com.cictec.middleware.core.mapper.BusMapper.selectBusByParams", params);
	}
	public List<Bus> selectListBusByParams(Map params) {
		return dao.selectList("com.cictec.middleware.core.mapper.BusMapper.selectBusByParams", params);
	}
	/**
	 * 【数据库】获取所有车辆信息,Map形式返回 key为mapKey
	 * @param params
	 * @param mapKey
	 * @return
	 */
	public Map<String, Bus> selectMapBusByParams(Map params, String mapKey) {
		return (Map<String, Bus>) dao.selectMap("com.cictec.middleware.core.mapper.BusMapper.selectBusByParams", params, mapKey);
//		return busMapper.selectMapBusByParams(params);
	}

	

	/**
	 * 更新当前车辆司机
	 * 
	 * @param busUuid
	 * @param drvUuid
	 * @param loginTime
	 */
	public void updateBusLoginDriver(String busUuid, String drvUuid, Date loginTime) {
		Bus bus = new Bus();
		bus.setBusUuid(busUuid);
		bus.setBusLoginDriverUuid(drvUuid);
		bus.setBusLoginTime(loginTime);

		busMapper.updateByPrimaryKeySelective(bus);
		logger.info("【busService】更新车辆表 {}, 司机{} 信息 ", busUuid, drvUuid);
	}

	/**
	 * 更新车辆的强制非运营状态
	 * 
	 * @param bus
	 */
	public void updateForceNonoperStatus(Bus bus) {
		Bus upBus = new Bus();
		upBus.setBusUuid(bus.getBusUuid());
		upBus.setBusForceNonOper(bus.getBusForceNonOper());
		upBus.setBusForceNonOperExpired(bus.getBusForceNonOperExpired());
		busMapper.updateByPrimaryKeySelective(upBus);

		logger.info("【busService】更新车辆表 {}, 设置 bus_force_non_oper :{} ", bus.getBusUuid(), bus.getBusForceNonOper());
	}

	/**
	 * 更新车辆所在线路
	 * 
	 * @param bus
	 */
	public void updateBusWorkingLine(Bus bus) {
		Bus upBus = new Bus();
		upBus.setBusUuid(bus.getBusUuid());
		upBus.setBusWorkingLineUuid(bus.getBusWorkingLineUuid());
		busMapper.updateByPrimaryKeySelective(upBus);

		logger.info("【busService】更新车辆表 {}, 设置 bus_working_line_uuid:{} ", bus.getBusUuid(),
				bus.getBusWorkingLineUuid());
	}

}
